﻿@inject IStringLocalizer<BootstrapBlazor.Shared.Samples.Notifications> Localizer
@inject NotificationService BrowserNotification

<div class="form-inline row g-3">
    <div class="col-12">
        <Checkbox ShowLabel="true" DisplayText="@Localizer["NotificationsNormalPermissionText"]" @bind-Value="@Permission" IsDisabled="true"></Checkbox>
    </div>
    <div class="col-12">
        <BootstrapInput ShowLabel="true" DisplayText="@Localizer["NotificationsNormalTitleText"]" @bind-Value="@Model.Title"></BootstrapInput>
    </div>
    <div class="col-12 ">
        <BootstrapInput ShowLabel="true" DisplayText="@Localizer["NotificationsNormalMessageText"]" @bind-Value="@Model.Message"></BootstrapInput>
    </div>
    <div class="col-12">
        <Checkbox ShowLabel="true" DisplayText="@Localizer["NotificationsNormalSilentText"]" @bind-Value="@Model.Silent"></Checkbox>
    </div>
    <div class="col-12">
        <Button Text="@Localizer["NotificationsNormalCheckPermissionText"]" OnClick="CheckPermission" Color="Color.Secondary"></Button>
        <Button Text="@Localizer["NotificationsNormalButtonText"]" OnClick="Dispatch" class="ms-2"></Button>
    </div>
</div>
<ConsoleLogger @ref="Logger" class="mt-3" />

@code {
    [NotNull]
    private ConsoleLogger? Logger { get; set; }

    private bool Permission { get; set; }

    private NotificationItem Model { get; set; } = new NotificationItem()
    {
        Icon = "_content/BootstrapBlazor.Shared/images/Argo-C.png"
    };

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        Model.Title ??= Localizer["NotificationsNormalTitleSampleText"];
        Model.Message ??= Localizer["NotificationsNormalMessageSampleText"];
        Model.OnClick = OnClickNotificationCallback;
    }

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await BrowserNotification.CheckPermission(false);
        }
    }

    private async Task CheckPermission()
    {
        Permission = await BrowserNotification.CheckPermission();
        Logger.Log(Localizer["NotificationsNormalGetPermissionCallbackText"] + (Permission ? "OK" : "No permission"));
    }

    private async Task Dispatch()
    {
        await BrowserNotification.Dispatch(Model);
    }

    private Task OnClickNotificationCallback()
    {
        Logger.Log($"{Localizer["NotificationsNormalOnClickText"]}");
        StateHasChanged();
        return Task.CompletedTask;
    }
}
